package com.niuwa.p2p.action.redenvelope;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.entity.redenvelope.RedEnvelopeRule;
import com.niuwa.p2p.entity.redenvelope.RedRange;
import com.niuwa.p2p.service.redenvelope.IRedEnvelopeRuleService;
import com.niuwa.p2p.vo.redenvelope.RedEnvelopeRuleVo;

public class RedRuleTmpltAction extends JsonAction {

	private static final long serialVersionUID = -164220153058789615L;
	
	@Autowired
	private IRedEnvelopeRuleService iRedEnvelopeRuleService;
	
	private RedEnvelopeRule envelopeRule;
	
	private RedEnvelopeRuleVo envelopeRuleVo;
	
	private List<RedRange> ranges;
	
	/** 规则编号 */
	private String ruleNo;
	
	/** 操作类型-启用-失效 */
	private String type;
	


	public String getRuleNo() {
		return ruleNo;
	}

	public void setRuleNo(String ruleNo) {
		this.ruleNo = ruleNo;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<RedRange> getRanges() {
		return ranges;
	}

	public void setRanges(List<RedRange> ranges) {
		this.ranges = ranges;
	}

	public RedEnvelopeRuleVo getEnvelopeRuleVo() {
		return envelopeRuleVo;
	}

	public void setEnvelopeRuleVo(RedEnvelopeRuleVo envelopeRuleVo) {
		this.envelopeRuleVo = envelopeRuleVo;
	}

	public RedEnvelopeRule getEnvelopeRule() {
		return envelopeRule;
	}

	public void setEnvelopeRule(RedEnvelopeRule envelopeRule) {
		this.envelopeRule = envelopeRule;
	}
	
 	
 	/**
 	 * 跳转到红包规则页面
 	 * @return
 	 */
 	@Action("gotoRedRuleList")
	public String gotoRedRuleList() {
		return "redRuleList";
	}
 	
	/**
	 * 规则列表
	 * @return
	 * @throws Exception
	 */
	@Action(value = "redRuleList")
	public String redRuleList() throws Exception {
		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();
		// 执行查询操作
		PageBean<RedEnvelopeRule> pagination = iRedEnvelopeRuleService.selectRedRuleByConditions(paramMap);
		setObjectReSult(pagination);
		return NONE;
	}

 	/**
 	 * 跳转到查看红包规则详情页面
 	 * @return
 	 */
 	@Action("showRedRuleDetails")
	public String showRedRuleDetails() {
 		RedEnvelopeRuleVo ruleVo = new RedEnvelopeRuleVo();
 		if(!StringUtil.isEmpty(ruleNo)){
 			ruleVo = iRedEnvelopeRuleService.selectRedRuleByRuleNo(ruleNo);
 			envelopeRule = ruleVo.getEnvelopeRule();
 			ranges = ruleVo.getRange();
 		}
		return "showRedRuleDetails";
	}
 	
 	/**
 	 * 跳转到添加红包规则页面
 	 * @return
 	 */
 	@Action("gotoAddRedRulePage")
	public String gotoAddRedRulePage() {
		return "addRedRule";
	}
 	
 	/**
 	 * 添加红包规则
 	 * @return
 	 */
 	@Action("addRedRule")
	public String addRedRule() {
 		RedEnvelopeRuleVo ruleVo = new RedEnvelopeRuleVo(); 
 		ruleVo.setEnvelopeRule(envelopeRule);
 		if(envelopeRuleVo != null){
 			if(envelopeRuleVo.getStartAmount() != null){
 	 			ruleVo.setStartAmount(envelopeRuleVo.getStartAmount());
 	 		}
 	 		if(envelopeRuleVo.getEndAmount() != null){
 	 			ruleVo.setEndAmount(envelopeRuleVo.getEndAmount());
 	 		}
 	 		if(envelopeRuleVo.getRedAmount() != null){
 	 			ruleVo.setRedAmount(envelopeRuleVo.getRedAmount());
 	 		}
 		}
 		JsonResult jsonResult = iRedEnvelopeRuleService.addRedEnvelopeRule(ruleVo);
 		this.setResult(jsonResult);
 		return NONE;
	}
 	
 	/**
 	 * 跳转到修改红包规则页面
 	 * @return
 	 */
 	@Action("gotoupdateRedRulePage")
	public String gotoupdateRedRulePage() {
 		RedEnvelopeRuleVo ruleVo = new RedEnvelopeRuleVo();
 		if(!StringUtil.isEmpty(ruleNo)){
 			ruleVo = iRedEnvelopeRuleService.selectRedRuleByRuleNo(ruleNo);
 			envelopeRule = ruleVo.getEnvelopeRule();
 			ranges = ruleVo.getRange();
 		}
		return "updateRedRule";
	}
 	
 	/**
 	 * 修改红包规则
 	 * @return
 	 */
 	@Action("updateRedRule")
	public String updateRedRule() {
 		RedEnvelopeRuleVo ruleVo = new RedEnvelopeRuleVo(); 
 		ruleVo.setEnvelopeRule(envelopeRule);
 		if(envelopeRuleVo != null){
 			if(envelopeRuleVo.getStartAmount() != null){
 	 			ruleVo.setStartAmount(envelopeRuleVo.getStartAmount());
 	 		}
 	 		if(envelopeRuleVo.getEndAmount() != null){
 	 			ruleVo.setEndAmount(envelopeRuleVo.getEndAmount());
 	 		}
 	 		if(envelopeRuleVo.getRedAmount() != null){
 	 			ruleVo.setRedAmount(envelopeRuleVo.getRedAmount());
 	 		}
 		}
 		JsonResult jsonResult = iRedEnvelopeRuleService.updateRedEnvelopeRule(ruleVo);
 		this.setResult(jsonResult);
		return NONE;
	}
 	
 	/**
 	 * 修改规则状态-启用-失效
 	 * @return
 	 */
	@Action("updateRedRuleStatus")
	public String updateRedRuleStatus() {
		JsonResult jsonResult = iRedEnvelopeRuleService.updateRedRuleStatus(ruleNo, type);
 		this.setResult(jsonResult);
		return NONE;
	}
	
	/**
 	 * 删除规则
 	 * @return
 	 */
	@Action("delRedRule")
	public String delRedRule() {
		JsonResult jsonResult = iRedEnvelopeRuleService.delRedRule(ruleNo);
 		this.setResult(jsonResult);
 		return NONE;
	}
	
}
